package com.canaan.business.controller.pc.inOutStock;

import java.util.List;

import com.canaan.business.common.enums.storehouse.StorehouseStockCheckStatusEnum;
import com.canaan.business.domain.storehouse.WmsStorehouseStock;
import com.canaan.business.service.storehouse.IWmsStorehouseStockService;
import com.canaan.common.exception.ServiceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.canaan.common.annotation.Log;
import com.canaan.common.core.controller.BaseController;
import com.canaan.common.core.domain.AjaxResult;
import com.canaan.common.enums.BusinessType;
import javax.annotation.Resource;
import com.canaan.business.domain.inOutStock.StorehouseStockCheck;
import com.canaan.business.service.inOutStock.IStorehouseStockCheckService;
import com.canaan.common.utils.poi.ExcelUtil;
import com.canaan.common.core.page.TableDataInfo;

/**
 * 待检仓库存Controller
 *
 * @author ruoyi
 * @date 2023-03-09
 */
@Api(tags = "待检仓库存")
@RestController
@RequestMapping("/inOutStock/stockCheck")
public class StorehouseStockCheckController extends BaseController {
    @Resource
    private IStorehouseStockCheckService storehouseStockCheckService;
    @Resource
    private IWmsStorehouseStockService storehouseStockService;
    /**
     * 查询待检仓库存列表
     */
    @ApiOperation(value = "查询待检仓库存列表")
    @GetMapping("/list")
    public TableDataInfo list(StorehouseStockCheck storehouseStockCheck) {
        startPage();
        List<StorehouseStockCheck> list = storehouseStockCheckService.selectStorehouseStockCheckList(storehouseStockCheck);
        return getDataTable(list);
    }

    /**
     * 导出待检仓库存列表
     */
    @ApiOperation(value = "导出待检仓库存列表")
    @Log(title = "待检仓库存", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StorehouseStockCheck storehouseStockCheck) {
        List<StorehouseStockCheck> list = storehouseStockCheckService.selectStorehouseStockCheckList(storehouseStockCheck);
        // 循环将状态转换为中文
        for (StorehouseStockCheck stockCheck : list) {
            stockCheck.setStatus(StorehouseStockCheckStatusEnum.getCodeByMessage(stockCheck.getStatus()));
        }
        ExcelUtil<StorehouseStockCheck> util = new ExcelUtil<StorehouseStockCheck>(StorehouseStockCheck.class);
        util.exportExcel(response, list, "待检仓库存数据");
    }

    /**
     * 获取待检仓库存详细信息
     */
    @ApiOperation(value = "获取待检仓库存详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(storehouseStockCheckService.selectStorehouseStockCheckById(id));
    }

    /**
     * 根据条码获取待检仓库存详细信息,如果获取不到就获取已检仓的库存条码
     */
    @ApiOperation(value = "获取待检仓库存详细信息")
    @GetMapping(value = "/getByBarcode/{barcode}")
    public AjaxResult getByBarcode(@PathVariable("barcode") String barcode) {
        StorehouseStockCheck check = storehouseStockCheckService.selectStorehouseStockCheckByBarcode(barcode);
        if (check == null) {
            WmsStorehouseStock storehouseStock = storehouseStockService.getCheckByBarcode(barcode);
            if (storehouseStock ==null ){
                throw new ServiceException("条码不存在");
            }else{
                return success(storehouseStock);
            }
        }
        return success(check);
    }

    /**
     * 新增待检仓库存
     */
    @ApiOperation(value = "新增待检仓库存")
    @Log(title = "待检仓库存", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StorehouseStockCheck storehouseStockCheck) {
        return toAjax(storehouseStockCheckService.insertStorehouseStockCheck(storehouseStockCheck));
    }

    /**
     * 修改待检仓库存
     */
    @ApiOperation(value = "修改待检仓库存")
    @Log(title = "待检仓库存", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StorehouseStockCheck storehouseStockCheck) {
        return toAjax(storehouseStockCheckService.updateStorehouseStockCheck(storehouseStockCheck));
    }

    /**
     * 删除待检仓库存
     */
    @ApiOperation(value = "删除待检仓库存")
    @Log(title = "待检仓库存", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(storehouseStockCheckService.deleteStorehouseStockCheckByIds(ids));
    }
}
